#include <glib.h>

#define SIZE       5
#define NUMBER_MIN 100
#define NUMBER_MAX 200

static guint32 array[SIZE];

void my_int_printf(gpointer data, gpointer user_data)
{
    g_printf("%s: value=%d user_data=%s\n", __FUNCTION__, GPOINTER_TO_INT(data), (char *)user_data);
}

gint my_int_cmp_func (gconstpointer d1, gconstpointer d2)
{
    gint i1, i2;

    i1 = GPOINTER_TO_INT (d1);
    i2 = GPOINTER_TO_INT (d2);

    return i1 - i2;
}


static void test_slist_sort_int(void)
{
    gint i = 0;
    GSList *slist = NULL;

    for(i=0;i<SIZE;i++) {
        slist = g_slist_append(slist,  GINT_TO_POINTER(array[i]));
    }

    g_printf("\n");
    g_printf("len=%d \n", g_slist_length(slist));
    g_slist_foreach(slist, my_int_printf, "before sort");

    slist = g_slist_sort(slist, my_int_cmp_func);
    g_slist_foreach(slist, my_int_printf, "after sort");

    g_slist_free(slist);
}

gint main (gint argc, gchar *argv[])
{
    gint i;

    g_test_init (&argc, &argv, NULL);

    for (i = 0; i < SIZE; i++) {
    array[i] = g_test_rand_int_range (NUMBER_MIN, NUMBER_MAX);
    }

    g_test_add_func ("/slist/sort_int", test_slist_sort_int);

    return g_test_run ();
}
